<template>
    <view class="content">
		<page-head :title="i18n.main.recharge"></page-head>
		<image src="../../static/tabbar/return.png" mode="" class="page-return" @tap="back"></image>
		
		<view class="center">
			<view class="verified">
				
				<view class="qrcode">
					<image :src="qrcode" @click="popup(qrcode)" mode="widthFix"></image>
				</view>
				
				<view class="form">
					<form style="width: 100%;">
						<view class="uni-padding-wrap" style="width: 100%;">
							<radio-group class="uni-flex" name="gender">
								<view class="uni-title">{{i18n.integral.prepaid}}</view>
								<view class="radio"  v-for="(li,index) in radio" :key="li.id">
									<radio @click="submit(index)" /><span>{{li}}</span>
								</view>
							</radio-group>
						</view>
					</form>
			
					
					<view class="form-input">
						<view class="text2">{{i18n.integral.actual}}：{{actual}}</view>
						<view class="text2">{{i18n.integral.image}}：</view>
						
						<block v-if="imageSrc">
								<image :src="imageSrc" class="images" mode="widthFix" @click="chooseImage"></image>
						</block>
						<block v-else>
							<view  class='img'>
								<view class="uni-hello-addfile" @click="chooseImage">{{i18n.integral.image}}</view>
							</view>
						</block>
						
						<m-input class="input bottom" type="password" displayable v-model="pass2" :placeholder="i18n.integral.pass" maxlength="6"></m-input>
					</view>
				</view>
				
				<view class="form1">
					<button class="btn-submit" @tap="transfer" v-if="status == 1">{{i18n.integral.recharge}}</button>
					<button class="btn-submit" v-else>{{i18n.integral.recharge}}</button>
				</view>
				
			</view>
		</view>
		
		<uni-popup :show="showPopup" type="middle" v-on:hidePopup="hidePopup">
			<view class='popup' @tap="shut">
				<image :src="img" mode="widthFix"></image>
			</view>
		</uni-popup>
		
		<image :src="back_img" mode="" class="back_img" :style="height"></image>
    </view>
    </view>
</template>

<script>
	import service from '../../service.js';
	import mInput from '../../components/m-input.vue';
	import pageHead from '../../components/page-head.vue';
		import uniPopup from "@/components/uni-popup.vue";
	export default {
		computed: {
			i18n () {
				return this.$t('index');
			}
		},
		components: {
			pageHead,
			uniPopup,
			mInput
        },
		data() {
			return {
				back_img:uni.getStorageSync('page'),
				height:uni.getStorageSync('height'),
				user:uni.getStorageSync('user'),
				qrcode:'',
				showPopup: false,
				radio:[],
				imageSrc:'',
				actual:0,
				number:'',
				lines:'',
				sale: 1,
				img:'',
				pass2:'',
				status:1
			};
		},
		onShow:function(){
			uni.setNavigationBarTitle({
				title: this.i18n.main.recharge
			});
			service.auth(this,  service.api.integral.recharge, {
				
			}, function(self, res) {
				self.qrcode = res.data.code;
				self.sale = res.data.rate;
				var list = res.data.credit;
				var split = ',';
				self.radio = list.split(split); //字符分割
			});
		},
		methods:{
			back(){
				uni.switchTab({
					url: '/pages/main/user'
				})
			},
			//统一的关闭popup方法
			hidePopup: function() {
				this.showPopup = false;
			},
			shut(){
				this.showPopup = false
			},
			popup(img){
				this.img = img;
				this.showPopup = true;
			},
			submit(num) {
				var radio = this.radio[num];
				this.number = radio;
				if(this.sale > 0){
					var amount = radio - this.lines;
					if(amount >= 0){
						var discount = this.sale / 100;
						radio = radio / discount;
					}
				}
				this.actual = Math.round(radio * 100) / 100;
			},
			//選擇圖片
			chooseImage: function() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album','camera'],
					success: (res) => {
						var imageSrc = res.tempFilePaths[0];
						this.imageSrc = imageSrc;
					},
					fail: (err) => {
						console.log('chooseImage fail', err)
					}
				})
			},
			transfer(){
				self = this;
				if(self.actual <= 0){
					uni.showToast({
						icon: 'none',
						title: self.i18n.integral.prepaid2
					});
					return;
				}
				if (self.imageSrc == '') {
					uni.showToast({
						icon: 'none',
						title: self.i18n.integral.image2
					});
					return;
				}
				if(self.pass2 != '' && self.pass2.length != 6){
					uni.showToast({
						icon: 'none',
						title: self.i18n.integral.pass2
					});
					return;
				}
				uni.showModal({
					content: this.i18n.integral.rechargetext,
					cancelText: this.i18n.main.cancel,
					confirmText: this.i18n.main.confirm,
					confirmColor: '#FFAE00',
					cancelColor: '#D2D2D2',
					success: function(res) {
						if (res.confirm) {
							self.status = 0;
							service.upload(this, service.api.integral.submit, {
								'credit1':self.number,
								'num':self.number,
								'money':self.actual,
								'change_pwd':self.pass2
							}, self.imageSrc, 'image', function(_self, res) {
								uni.showToast({
									icon: 'none',
									title: res.info
								});
								if(res.code == 1){
									setTimeout(function(){
										uni.navigateBack({
											delta: 1
										});
									}, 1000);
								}else{
									self.status = 1;
								}
							});
						}
					}
				});
			}
		}
    }
</script>

<style lang="scss">
	.content{
		padding: 0;
		background: #F2F2F2;
	}
	.center{
		z-index: 30;
		width: calc(100% - 60upx);
		margin: 0 30upx;
	}
	.verified{
		padding: 40upx 0;
		border-radius: 20upx;
		margin: 30upx 0 60upx;
		background: rgba(255,255,255,.7);
	}
	.qrcode{
		top: 40upx;
		width: 50%;
		margin: 0 25%;
	}
	.qrcode image{
		width: 100%;
	}

	.popup image {
		width: 100%;
	}
	.radio{
		width: 33.3%;
		height: 60upx;
		/* float: left; */
	}
	.uni-flex{
		display: flex;
        flex-wrap: wrap;
		flex-direction: row;
		display: inline-flex;
		margin: 50upx 60upx 0; 
		width: calc(100% - 120upx);
	}
	.img{
		color: #DDD;
		width: 100%;
		height: 300upx;
		font-size: 40upx;
		text-align: center;
		line-height: 300upx;
		background-color: #fff;
		border: 4upx dashed #DDD;
	}
	.images{
		width: 100%;
	}
	.uni-title{
		width: 100%;
		margin-bottom: 20upx;
	}
	.text2{
		margin: 0 5% 20upx;
		font-size: 32upx;
	}
	.current{
		color: #929292;
		font-size: 32upx;
		line-height: 60upx;
		margin: 30upx 60upx 0;
	}
	.current text{
		color: #00B26A;
		font-size: 36upx;
		line-height: 60upx;
	}
	.reminder{
		color: red;
		margin: 0 40upx;
		padding: 10upx 0;
		font-size: 26upx;
		text-align: center;
		line-height: 30upx;
		background: #ffe9e9;
	}
	.form{
		display: flex;
        flex-wrap: wrap;
		flex-direction: row;
		display: inline-flex;
		width: 100%;
		margin-top: 40upx;
		border-top: 2upx solid #BBBBBB;
	}
	.form-input{
		width: calc(100% - 120upx);
		margin: 30upx 60upx 0;
	}
	.form-input .input{
		height: 60upx;
		margin: 30upx 0 0;
		border-radius: 20upx;
		padding: 10upx 20upx;
		width: calc(100% - 40upx);
		background: rgba(250,49,161,.2);
	}
	.form-input .bottom{
		margin-bottom: 30upx;
	}
	.btn-submit{
		height: 80upx;
		line-height: 80upx;
		margin: 30upx 20% 0;
	}
	.back_img{
		margin: 0;
		height: 100%;
	}
</style>